$(document).ready(function(){
    $.ajax({
        url: "/index/getMenu",
        type: 'get',
        async: false,
        success: function (data) {
            var data = data.data;
            if (data != null) {

                var z = 0;//用来转化成数字的中介
                // 导航栏Collections的下拉导航
                var collectionNav_li = "";
                //搜索框左边的分类
                var collectionOpt = "";
                for (var d in data) {
                    collectionNav_li += "<li> <a href=\"collection.html?id=" + data[d].id + "\" class=\"site-nav__link\">" + data[d].name + "</a> </li>";
                    collectionOpt += '<option value="' + data[d].id + '">' + data[d].name + '</option>';
                }
                $("#collectionNav").append(collectionNav_li);
                $("#collection-option").append(collectionOpt);

            }

        }

    });

    //主列表
    $.ajax({
        url:"http://localhost:8092/agriecom/news/list",
        dataType:"json",
        type:"GET",
        success:function(data){
            let parentDiv=$(".large--three-quarters");
            $.each(data,function (i, ele) {
                $($(".blog-image a")[i]).attr("href","http://localhost:8092/agriecomPage/news/forward?newsId="+ele.id);
                $($(".blog-image a img")[i]).attr("src",ele.pic);
                //title
                $($(".blog-title")[i]).html("");
                $($(".blog-title")[i]).html("<a href=\"http://localhost:8092/agriecomPage/news/forward?newsId="+ele.id+"\">"+ele.title+"</a>");
                //author
                var date = new Date(ele.createDate);
                $($(".author")[i]).html("");
                $($(".author")[i]).html("Posted by <strong>"+ele.source+"</strong> on <time>"+date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()+"</time>");
                //brief
                $($(".blog-content p")[i]).html("");
                $($(".blog-content p")[i]).html(ele.brief);
                //read more
                $($(".category-blogs>p:last-child")[i]).html("");
                $($(".category-blogs>p:last-child")[i]).html("<a class=\"readmore btn\" href=\"http://localhost:8092/agriecomPage/news/forward?newsId="+ele.id+"\">阅读详情 →</a>");

                var childDiv=$(".large--three-quarters>div:first-child").clone(false,true);
                parentDiv.append(childDiv);

            })
            $(".large--three-quarters>div:last-child").empty();
        }
    })
    //左侧列表
    $.ajax({
      url:"http://localhost:8092/agriecom/news/mostLatestNews",
      dataType:"json",
      type: "GET",
      success:function(data){
        var lis=$(".sb-content li")
        $.each(data,function (i, ele) {
            console.log();
            $(lis[i]).children("div").html("");
            $(lis[i]).children("div").html("<a href=\"http://localhost:8092/agriecomPage/news/forward?newsId="+ele.id+"\">"+ele.title+"</a>");
            $(lis[i]).children("time").html("");
            var date = new Date(ele.createDate);
            $(lis[i]).children("time").html(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());

        })
      }
    })
})
